package com.nwpu.bigdata.service.impl;
/**
 *@version:2018年6月3日下午10:23:52
*/
/**
 * @author Administrator
 *
 */


import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParser;
import com.nwpu.bigdata.dto.PodInfo;
import com.nwpu.bigdata.dto.SoftwareStatus;
import com.nwpu.bigdata.service.PodInfoService;

import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodList;
import io.fabric8.kubernetes.client.KubernetesClient;



/**
 *@version:2018年5月28日下午9:05:07
*/
/**
 * @author Administrator
 *
 */
@Service
public class PodInfoServiceImpl implements PodInfoService{
	
	
private static final Logger logger = LoggerFactory.getLogger(PodInfoServiceImpl.class);


public  List<SoftwareStatus> getPodInfoList(KubernetesClient kubernetesClient){
	
	 List<PodInfo> podInfos = new ArrayList<PodInfo>();
	 System.out.println("k8s是"+kubernetesClient.getMasterUrl());
	 if(kubernetesClient!=null){
		 //得到pod的podlist
		 PodList podList = kubernetesClient.pods().list();
		 //将podlist中的内容放入list列表中
         List<Pod> pods =  podList.getItems(); 
         System.out.println(pods);
        //遍历列表提取出每一个列表的pod名pod所在host的ip以及每一个pod的状态
         for( Pod pod:pods){ 
        	 
        	 PodInfo podInfo = new PodInfo();
        	 
        	 String podName = pod.getMetadata().getName();
        	
        	 podInfo.setPodName(podName);
        	 
        	 String hostIp = pod.getStatus().getHostIP();
        	 
        	 podInfo.setHostIp(hostIp);
        	 
        	 String phase = pod.getStatus().getPhase(); 
        	 pod.getStatus().getPhase();
        	 podInfo.setPhase(phase);
        	 
        	 podInfos.add(podInfo);
             logger.info("pod", podInfos);
               
         }
         
     }
	 
	 //创建一个map用于存放每一个组件的名称以及组件所在pod（pod为running/pending状态的pod个数，如果个数大于1则表示这个组件的状态是正常的，如果小于1则表示这个组件没有启动）
	 Map<String,Integer> map = new HashMap<String,Integer>();
	 map.put("cassandra", 0);
	 map.put("neo4j", 0);
	 map.put("iotdb", 0);
	 map.put("mysql", 0);
	 map.put("nosqldb", 0);
	 for(PodInfo podInfo : podInfos){
		 System.out.println(podInfo);
			//if(podInfo.getPodName().contains("cassandra-ss")){  //11.13
			if(podInfo.getPodName().contains("kvstore-v1-0")){
				if(podInfo.getPhase().equals("Running") || podInfo.getPhase().equals("Pending")){
					int c = map.get("cassandra");
					c++;
					map.replace("cassandra", c);
				}
			//}else if(podInfo.getPodName().contains("neo4j-ss")){
			}else if(podInfo.getPodName().contains("ingraphdb-v1-0")){
				
				if(podInfo.getPhase().equals("Running") || podInfo.getPhase().equals("Pending")){
					int c = map.get("neo4j");
					c++;
					map.replace("neo4j", c);
				}
			//}else if(podInfo.getPodName().equals("mysql")){
			}else if(podInfo.getPodName().equals("kingbase")){
					if(podInfo.getPhase().equals("Running") || podInfo.getPhase().equals("Pending")){
						int c = map.get("mysql");
						c++;
						map.replace("mysql", c);
					}
			}else if(podInfo.getPodName().equals("iotdb-server")){
				if(podInfo.getPhase().equals("Running") || podInfo.getPhase().equals("Pending")){
					int c = map.get("iotdb");
					c++;
					map.replace("iotdb", c);
				}
		    }else if(podInfo.getPodName().equals("nosqldb")){
				if(podInfo.getPhase().equals("Running") || podInfo.getPhase().equals("Pending")){
					int c = map.get("nosqldb");
					c++;
					map.replace("nosqldb", c);
				}
		    }
			
		}
	 List<SoftwareStatus> list = new ArrayList<SoftwareStatus>();
	 //判断组件有没有启动
	 for(Map.Entry<String,Integer> entry: map.entrySet()){
		 SoftwareStatus software = new SoftwareStatus();
		 String status;
		 String name =  entry.getKey();

		 if( entry.getValue() >= 1){
			 status = "running";
		 }
		 else status = "down";
		 
		 software.setSoftwareName(name);
		 software.setStatus(status);
		 list.add(software);
	  }
	   kubernetesClient.close();
	   return list; 
	
}

//用于查询每一个组件相关的pod的运行状态
public List<PodInfo> getSoftwarePod(KubernetesClient kubernetesClient,String software){
	 List<PodInfo> podInfos = new ArrayList<PodInfo>();

     if(kubernetesClient!=null){//判断是否建立连接
		 
		 PodList podList = kubernetesClient.pods().list();
		 
         List<Pod> pods =  podList.getItems(); //得到所有pod的信息并将其存入list中        
         for( Pod pod:pods){ 
        	 
        	 PodInfo podInfo = new PodInfo();
        	 
        	 String podName = pod.getMetadata().getName(); //获取pod的名字
        	
        	 podInfo.setPodName(podName);
        	 
        	 String hostIp = pod.getStatus().getHostIP(); //获取pod所在的主机Ip
        	 
        	 podInfo.setHostIp(hostIp);
        	 
        	 String phase = pod.getStatus().getPhase(); //获取pod的运行状态
        	 pod.getStatus().getPhase();
        	 podInfo.setPhase(phase);
        	 
        	 podInfos.add(podInfo);
        	 
        	 logger.info("softwarepod", podInfos);
             //System.out.println("显示一个pod信息==============================================="+ pod.getMetadata().getName()+pod.getStatus().getHostIP()+ pod.getStatus().getPhase());    
               
         }
	
        }
     
	 List<PodInfo> softwarePods = new ArrayList<PodInfo>();
     for(PodInfo podInfo : podInfos){
    	
    	 if(podInfo.getPodName().contains(software)){
    		 //if(software == "mysql" || software == "iotdb-server" || software == "nosqldb"){
    		 /*if(software == "mysql" || software == "iotdb-server" || software == "nosqldb" || software == "kvstore" || software == "ingraphdb"){
    			 if(podInfo.getPodName().equals(software)){
    				 softwarePods.add(podInfo);
    			 }
    		 }else{
    			 softwarePods.add(podInfo);
    		 }*/
    		 softwarePods.add(podInfo);
    	 }
     }
     
     kubernetesClient.close();
     return softwarePods;
}
}
